
#include <string.h>
#include <stdio.h>

#include "user_dfu_m_cfg.h"
#include "gr5513_ota_m.h"
/*
 * LOCAL FUNCTION DECLARATION
 ****************************************************************************************
 */

#define fw_pwd "ble_fw/ble_app_beacon_fw.bin"
extern int signal_received;
extern bool ota_type;
static void dfu_m_get_img_info(dfu_img_info_t *p_img_info);
static void dfu_m_get_img_data(uint32_t addr, uint8_t *p_data, uint16_t length);
static void dfu_m_event_handler(dfu_m_event_t event, uint8_t pre);
/*
 * LOCAL VARIABLE DEFINITIONS
 ****************************************************************************************
 */
static dfu_m_func_cfg_t s_dfu_m_func_cfg = 
{
    .dfu_m_get_img_info  = dfu_m_get_img_info,
    .dfu_m_get_img_data  = dfu_m_get_img_data,
    .dfu_m_send_data     = ble_socket_send_data,
    .dfu_m_event_handler = dfu_m_event_handler,
};

//static dfu_img_info_t dfu_img_info;

/*
 * LOCAL FUNCTION DEFINITIONS
 ****************************************************************************************
 */

static void dfu_m_get_img_info(dfu_img_info_t *img_info)
{
    FILE *info_addr;
    static uint8_t info_data[48] = {0};
    size_t err = -1;

    //printf("%s\n",__func__);
	info_addr = fopen(fw_pwd, "rb+"); 
	if (NULL == info_addr)
	{
		printf("文件打开错误\n");
	}
    fseek(info_addr,-48L,SEEK_END);
    err = fread(info_data,sizeof(uint8_t),48,info_addr);
    if(err >= 0)
    {
/*         printf("fread buff= [ ");
        for (uint16_t i = 0; i < 48; i++)
            printf("%02x ", (uint8_t)info_data[i]);
        printf(" ]\r\n"); */
        memcpy(img_info, info_data, sizeof(info_data));
        printf("pattern = %04X version = %04X load_addr = %08X\n", img_info->pattern, img_info->version,img_info->boot_info.load_addr);
        if (!ota_type)
        {
            if (img_info->version, img_info->boot_info.load_addr != DFU_FW_load_addr)
            {
                img_info->version, img_info->boot_info.load_addr = DFU_FW_load_addr;
            }
        }
        rewind(info_addr);
        fclose(info_addr);
    }else{
        rewind(info_addr);
        printf("文件读取失败 err = %d\n",err);
        fclose(info_addr);
    }
}

static void dfu_m_get_img_data(uint32_t addr, uint8_t *p_data, uint16_t length)
{
    FILE *FW_addr;
    size_t err = -1;

    //printf("%s\n",__func__);
	FW_addr = fopen(fw_pwd, "rb+"); 
	if (NULL == FW_addr)
	{
		printf("文件打开错误\n");
	}
    fseek(FW_addr,addr,SEEK_SET);
    err = fread(p_data, sizeof(uint8_t), length, FW_addr);
    if (err < 0)
    {
        printf("fread err!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\n");
    }
    fclose(FW_addr);
    return;
}

static void dfu_m_event_handler(dfu_m_event_t event, uint8_t pre)
{
    //printf("%s event = %d\n",__func__,event);
    switch(event)
    {
        case PRO_START_SUCCESS:
            break;

        case PRO_FLASH_SUCCESS:
            break;

        case PRO_END_SUCCESS:
        signal_received = 5050 ;
        printf("%s PRO_END_SUCCESS = %d\n",__func__,event);
            break;

        default:
            break;
    }
}



/*
 * GLOBAL FUNCTION DEFINITIONS
 *****************************************************************************************
 */
void user_dfu_m_init( uint16_t once_send_size)
{
    dfu_m_init(&s_dfu_m_func_cfg, once_send_size);
}


